<template>
  <div class="searchTop">
    <div class="searchTopNav">
      <div
        class="back"
        @click="$router.go(-1)"
      >
        <svg
          class="icon"
          aria-hidden="true"
        >
          <use xlink:href="#icon-zuojiantou-copy"></use>
        </svg>
      </div>
      <div class="right">
        <input
          type="text"
          :placeholder="placeholder"
          @keydown.enter="saveKeyWord"
          v-model="searchKeyword"
        >
      </div>
    </div>
    <div
      class="history"
      v-show="historyShow"
    >
      <div class="historyLeft">历史</div>
      <div class="historyRight">
        <div
          class="keywordItem"
          v-for="(item,i) in keywordList"
          :key="i"
          @click="historySearch(item)"
        >{{item}}</div>
      </div>
    </div>
    <div
      class="searchSongs"
      v-show="searchSongs.length!==0"
    >
      <div class="searchSongs-top">
        <div class="left">
          <svg
            class="icon"
            aria-hidden="true"
          >
            <use xlink:href="#icon-bofang2"></use>
          </svg>
          <div class="text">
            <div class="title">播放全部</div>
            <div class="num">（共{{searchSongs.length}}首）</div>
          </div>
        </div>
        <div class="btn">
          + 收藏
        </div>
      </div>
      <div class="list">
        <div
          class="playItem"
          v-for="(item,index) in searchSongs"
          :key="item.id"
        >
          <div class="left">
            <div class="index">{{index+1}}</div>
            <div class="content">
              <div class="title">{{item.name}}</div>
              <div class="tags">
                <span class="author">
                  {{item.album.name}}
                </span>
              </div>
            </div>
          </div>
          <div class="right">
            <svg
              class="icon"
              aria-hidden="true"
              @click="setPlay(item,index)"
            >
              <use xlink:href="#icon-bofang1"></use>
            </svg>
            <svg
              class="icon"
              aria-hidden="true"
            >
              <use xlink:href="#icon-liebiao"></use>
            </svg>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {mapMutations} from 'vuex'
  import {searchMusic} from '@/api'
  export default {
    data() {
      return {
        placeholder:'陈奕迅',
        keywordList:[],
        searchKeyword:'',
        searchSongs:[],
        historyShow:true
      }
    },
    beforeMount() {
      this.keywordList = localStorage.keywordList?JSON.parse(localStorage.keywordList) : [];
    },
    watch: {
      searchKeyword(val){
        if(val==''){
          this.historyShow = true;
          this.searchSongs = [];
        }
      }
    },
    setup() {
      function changeValue(num){
       let res = 0;
       if(num>=100000000){
         res = num/100000000;
         res = res.toFixed(2) + '亿';
       }else if(num>=10000){
         res = num/10000;
         res = res.toFixed(2) + '万';
       }
       return res;
     }
     return {
       changeValue
     }
    },
    methods: {
      ...mapMutations(['setPlayIndex']),
      historySearch(keyword){
        this.searchKeyword = keyword;
        this.saveKeyWord();
        this.historyShow = false;
      },
      setPlay(item,i){
        item.al = item.album;
        item.al.picUrl = item.album.artist.img1v1Url;
        this.$store.commit('pushPlaylist',item);
        // this.commit('setPlayIndex',this.$store.state.playlist.length-1)
        this.setPlayIndex(this.$store.state.playlist.length-1);
      },
      async saveKeyWord() {
        this.keywordList.push(this.searchKeyword);
        this.keywordList = [...new Set(this.keywordList)];
        if(this.keywordList.length>10){
          this.keywordList = this.keywordList.slice(this.keywordList.length-10,this.keywordList.length);
        }
        localStorage.keywordList = JSON.stringify(this.keywordList);
        let result = await searchMusic(this.searchKeyword);
        console.log(result);
        this.searchSongs = result.data.result.songs;
      }
    },
  }
</script>

<style lang="less" scoped>
.searchTop {
  width: 7.5rem;
  padding: 0 0.2rem;
  height: calc(100vh - 1.2rem);
  .searchTopNav {
    display: flex;
    width: 100%;
    height: 1.2rem;
    align-items: center;
    position: fixed;
    top: 0;
    left: 0;
    background-color: #fff;
    padding: 0 0.4rem;
    .icon {
      width: 0.4rem;
      height: 0.4rem;
      fill: #333;
    }
    .right {
      padding: 0 0 0 0.4rem;
      flex: 1;
      input {
        width: 100%;
        border: none;
        outline: none;
        border-bottom: 1px solid #666;
        height: 0.5rem;
      }
    }
  }
  .history {
    display: flex;
    align-items: flex-start;
    margin-top: 1.2rem;
    .historyLeft {
      width: 1.2rem;
      height: 0.6rem;
      line-height: 0.6rem;
      font-weight: 900;
      margin: 0.2rem 0;
    }
    .historyRight {
      color: #666;
      display: flex;
      flex-wrap: wrap;
      flex: 1;
      .keywordItem {
        background-color: #eee;
        height: 0.6rem;
        padding: 0 0.2rem;
        border-radius: 0.4rem;
        line-height: 0.6rem;
        margin: 0.2rem 0.1rem;
      }
    }
  }
  .searchSongs {
    // width: 7.5rem;
    // padding: 0 0.4rem;
    background-color: #fff;
    border-top-left-radius: 0.3rem;
    border-top-right-radius: 0.3rem;
    position: fixed;
    top: 1.2rem;
    left: 0.2rem;
    right: 0.2rem;
    bottom: 1.2rem;
    overflow: scroll;
    // margin-top: 0.4rem;
    // padding-bottom: 1.2rem;
    .searchSongs-top {
      display: flex;
      justify-content: space-between;
      height: 1rem;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .icon {
          width: 0.4rem;
          height: 0.4rem;
        }
        .text {
          display: flex;
          align-items: center;
          margin-left: 0.2rem;
          .title {
            font-size: 0.34rem;
            font-weight: 600;
          }
          .num {
            font-size: 0.24rem;
            color: #666;
          }
        }
      }
      .btn {
        font-size: 0.24rem;
        color: #fff;
        background-color: orangered;
        line-height: 0.6rem;
        padding: 0.1rem;
        border-radius: 0.4rem;
      }
    }
    .list {
      .playItem {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 1rem;
        .left {
          display: flex;
          align-items: center;
          color: #666;
          .index {
            width: 0.4rem;
          }
          .content {
            margin-left: 0.4rem;
            max-width: 4.5rem;
            .title {
              font-size: 0.3rem;
              font-weight: 900;
              color: #000;
              margin-bottom: 0.1rem;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
            }
            .tags {
              display: flex;
              align-items: center;
              .tag {
                font-size: 0.2rem;
                color: orangered;
                border: 1px solid orangered;
                border-radius: 0.1rem;
                flex-shrink: 0;
                margin-right: 0.1rem;
                padding: 0 0.05rem;
              }
              .author {
                color: #666;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
                display: inline-block;
              }
            }
          }
        }
        .right {
          .icon {
            margin: 0 0.2rem;
          }
        }
      }
    }
  }
}
</style>